In [1]:
import tensorflow as tf
import numpy as np

Pick an easy example and do it in Numpy


In [2]:
f1 = np.array([[1.,2.,3.],[4.,5.,6.]])
f1


Out[2]:
array([[ 1.,  2.,  3.],
       [ 4.,  5.,  6.]])

In [3]:
f2 = np.array([[7.,8.,9.],[10.,11.,12.]])
f2


Out[3]:
array([[  7.,   8.,   9.],
       [ 10.,  11.,  12.]])

In [4]:
np.sum(f1*f2)


Out[4]:
217.0

In [5]:
np.matmul(f1.flatten(),np.transpose(f2.flatten()))


Out[5]:
217.0

In [6]:
F = np.array([f1,f2])
print F.shape
F


(2, 2, 3)
Out[6]:
array([[[  1.,   2.,   3.],
        [  4.,   5.,   6.]],

       [[  7.,   8.,   9.],
        [ 10.,  11.,  12.]]])

In [7]:
np.sum(np.square(f2.flatten()))


Out[7]:
559.0

In [8]:
F_flat = F.reshape(F.shape[0],F.shape[1]*F.shape[2])
F_flat


Out[8]:
array([[  1.,   2.,   3.,   4.,   5.,   6.],
       [  7.,   8.,   9.,  10.,  11.,  12.]])

In [9]:
np.matmul(F_flat,F_flat.T)


Out[9]:
array([[  91.,  217.],
       [ 217.,  559.]])

In [10]:
np.dot(F_flat,F_flat.T)


Out[10]:
array([[  91.,  217.],
       [ 217.,  559.]])

slightly tougher example


In [11]:
f1 = np.array([[1.,2.],[3.,4.],[5.,6.]])
f2 = np.array([[7.,8.],[9.,10.],[11.,12.]])
f3 = np.array([[13.,14.],[15.,16.],[17.,18.]])
F = np.array([f1,f2,f3])
F.shape


Out[11]:
(3, 3, 2)

In [12]:
F_flat = F.reshape(F.shape[0],F.shape[1]*F.shape[2])
F_flat


Out[12]:
array([[  1.,   2.,   3.,   4.,   5.,   6.],
       [  7.,   8.,   9.,  10.,  11.,  12.],
       [ 13.,  14.,  15.,  16.,  17.,  18.]])

In [13]:
F_flat.T


Out[13]:
array([[  1.,   7.,  13.],
       [  2.,   8.,  14.],
       [  3.,   9.,  15.],
       [  4.,  10.,  16.],
       [  5.,  11.,  17.],
       [  6.,  12.,  18.]])

In [14]:
np.matmul(F_flat,F_flat.T)


Out[14]:
array([[   91.,   217.,   343.],
       [  217.,   559.,   901.],
       [  343.,   901.,  1459.]])

In [15]:
np.dot(F_flat,F_flat.T)


Out[15]:
array([[   91.,   217.,   343.],
       [  217.,   559.,   901.],
       [  343.,   901.,  1459.]])

TensorFlow


In [16]:
def Gram_Matrix(F_batch):
    F = F_batch[0]
    s = tf.shape(F)
    F_flat = tf.reshape(F,shape=(s[0],s[1]*s[2]))
    G = tf.matmul(F_flat,tf.transpose(F_flat))
    return G

In [17]:
g = tf.Graph()
with g.as_default():
    F = tf.placeholder(tf.float32)
    G = Gram_Matrix(F)

In [18]:
sess = tf.Session(graph = g)
writer = tf.train.SummaryWriter('./logs/',graph=g)

F_in = np.array([[f1,f2,f3]])
fd = {F : F_in}
sess.run(G,feed_dict=fd)


WARNING:tensorflow:From <ipython-input-18-1126d643465b>:2 in <module>.: __init__ (from tensorflow.python.training.summary_io) is deprecated and will be removed after 2016-11-30.
Instructions for updating:
Please switch to tf.summary.FileWriter. The interface and behavior is the same; this is just a rename.
Out[18]:
array([[   91.,   217.,   343.],
       [  217.,   559.,   901.],
       [  343.,   901.,  1459.]], dtype=float32)

With Keras


In [19]:
from keras import backend as K
def gram_matrix(x):
    features = K.batch_flatten(K.permute_dimensions(x,(2,0,1)))
    gram = K.dot(features, K.transpose(features))
    return gram


Using TensorFlow backend.

In [23]:
with g.as_default():
    F = tf.placeholder(tf.float32)
    G_keras = gram_matrix(F)

In [24]:
# sess = tf.Session(graph = g)
# writer = tf.train.SummaryWriter('./logs/',graph=g)

F_in = np.array([[f1,f2,f3]])
fd = {F : F_in}
sess.run(G_keras,feed_dict=fd)


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-24-2754eb1b8eca> in <module>()
      4 F_in = np.array([[f1,f2,f3]])
      5 fd = {F : F_in}
----> 6 sess.run(G_keras,feed_dict=fd)

/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    764     try:
    765       result = self._run(None, fetches, feed_dict, options_ptr,
--> 766                          run_metadata_ptr)
    767       if run_metadata:
    768         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    962     if final_fetches or final_targets:
    963       results = self._do_run(handle, final_targets, final_fetches,
--> 964                              feed_dict_string, options, run_metadata)
    965     else:
    966       results = []

/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1012     if handle is None:
   1013       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
-> 1014                            target_list, options, run_metadata)
   1015     else:
   1016       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
   1032         except KeyError:
   1033           pass
-> 1034       raise type(e)(node_def, op, message)
   1035 
   1036   def _extend_graph(self):

InvalidArgumentError: transpose expects a vector of size 4. But input(1) is a vector of size 3
	 [[Node: transpose_3 = Transpose[T=DT_FLOAT, Tperm=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](_recv_Placeholder_1_0/_9, transpose_3/perm)]]
	 [[Node: MatMul_2/_15 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_3_MatMul_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op u'transpose_3', defined at:
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/runpy.py", line 174, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/traitlets/config/application.py", line 653, in launch_instance
    app.start()
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tornado/ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-23-76a8827755d7>", line 3, in <module>
    G_keras = gram_matrix(F)
  File "<ipython-input-19-351219881f11>", line 3, in gram_matrix
    features = K.batch_flatten(K.permute_dimensions(x,(2,0,1)))
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 1476, in permute_dimensions
    return tf.transpose(x, perm=pattern)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 1355, in transpose
    ret = gen_array_ops.transpose(a, perm, name=name)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3656, in transpose
    result = _op_def_lib.apply_op("Transpose", x=x, perm=perm, name=name)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op
    op_def=op_def)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2240, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/floptical/anaconda2/envs/tensorflow/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1128, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): transpose expects a vector of size 4. But input(1) is a vector of size 3
	 [[Node: transpose_3 = Transpose[T=DT_FLOAT, Tperm=DT_INT32, _device="/job:localhost/replica:0/task:0/gpu:0"](_recv_Placeholder_1_0/_9, transpose_3/perm)]]
	 [[Node: MatMul_2/_15 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_3_MatMul_2", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

In [25]:
F_in.shape


Out[25]:
(1, 3, 3, 2)

In [ ]: